'''
@Company: TWL
@Author: xue jian
@Email: xuejian@kanzhun.com
@Date: 2020-03-16 19:56:00
'''
class Solution:
    def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:
        def find_speed_group(l, speed, k):
            if k == 1:
                return []
            import heapq as hq
            re = []
            for ind in l:
                if len(re)>=k-1:
                    hq.heappush(re, speed[ind])
                    hq.heappop(re)
                else:
                    hq.heappush(re, speed[ind])
            return re
        speed_ind = {}
        eff_ind = {}
        self.speed_ind = {ind:val for ind, val in enumerate(speed)}
        # print(speed_ind)
        eff_ind = {val:ind for ind, val in enumerate(efficiency)}
        efficiency.sort()
        # print(efficiency)
        re = 0
        use_l = [eff_ind[e] for e in efficiency]
        for i in range(n-k+1):
            # print(i)
            tmp_l = use_l[i+1:]
            # print('tmp_l', tmp_l)
            speed_group = find_speed_group(tmp_l, speed, k)
            speed_group.append(speed[use_l[i]])
            # print(speed_group)
            re = max(re, sum(speed_group)*efficiency[i])
        return re